<template>
  <div class="login-container">
    <div class="login-box">
      <div class="touxiang-box">
        <img src="../assets/logo.png" alt="">
      </div>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 用户输入时的验证
// 1. 给el-form绑定rules规则对象
// 2. 在data上定义rules规则对象
// 3. 在el-for-item上添加prop
// 注意 prop的值是字符串  el-form上的model必须要有

// router 是路由对象 控制整个项目的路由对象
// route 是当前这个页面的路由相关信息 
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456"
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" }
        ]
      }
    };
  },
  mounted(){
    console.log(this.$router);
    console.log(this.$route);
  },
  methods: {
    // 重置登录表单
    resetLoginForm() {
      // 获取el-form组件对象 调用resetFields方法重置表单内容
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      //在发送请求之前要对数据进行预校验 如果true才能请求ajax
      this.$refs.loginFormRef.validate(async v => {
        if (v) {
          // 使用异步函数(async/await)
          // 可以把异步的操作 转化为 同步的书写形式 就可以获取到服务器返回的值了
          const { data: res } = await this.$http.post("login", this.loginForm);
          if(res.meta.status == 200){
            console.log('成功')
            this.$message.success('成功')
            window.sessionStorage.setItem('token',res.data.token)
            this.$router.push('/home')
          }else{
            this.$message.error('失败')
          }
        } else {
          this.$refs.loginFormRef.resetFields();
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.login-container {
  background-color: #2b4b6b;
  height: 100%;
}
.login-box {
  width: 450px;
  height: 300px;
  position: absolute;
  background-color: #fff;
  border-radius: 3px;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .touxiang-box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>